//
//  HideAndSeekTapScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/7/13.
//
//

#include "HideAndSeekTapScene.h"
#include "GameLayerManager.h"
#include "MySpecialDisappearButton.h"
#include "SceneManager.h"
#include "MyRectObject.h"
CCScene* HideAndSeekTapScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HideAndSeekTapScene *layer = HideAndSeekTapScene::create(numberOfSeconds);
    GameLayerManager::sharedInstance()->hideAndSeekTapLayer = layer;
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

HideAndSeekTapScene* HideAndSeekTapScene::create(int numberOfSeconds){
    HideAndSeekTapScene* scene = HideAndSeekTapScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}

bool HideAndSeekTapScene::init(){
    CountDownGameScene::init();
    srand (time(NULL));
    lastButtonRect = CCRectZero;
    buttonArray = CCArray::create();
    buttonArray->retain();
    return true;
}


void HideAndSeekTapScene::reset(){
    CountDownGameScene::reset();
    for(int i=0;i<buttonArray->count();i++){
        MySpecialDisappearButton* button = (MySpecialDisappearButton*)buttonArray->objectAtIndex(i);
        this->removeChild(button);
    }
    buttonArray->removeAllObjects();

}

void HideAndSeekTapScene::startGame(){
    CountDownGameScene::startGame();
    createRandomButtonsOnStart(3);
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->hideAndSeekScene;

}

void HideAndSeekTapScene::createRandomButtonOnScreen(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.2f;
    int maxX = winSize.width - buttonSize;
    int maxY = winSize.height - scoreAndTimeLayer->getBarHeight() - buttonSize;
    int posX;
    int posY;
    CCRect currentRect;
    do {
        posX = rand()%maxX;
        posY = rand()%maxY;
        currentRect = CCRectMake(posX, posY, buttonSize, buttonSize);
    } while (currentRect.intersectsRect(lastButtonRect));
    lastButtonRect = currentRect;
    MySpecialDisappearButton* button = MySpecialDisappearButton::create("circle_button.png", posX, posY, buttonSize, buttonSize);
    CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(HideAndSeekTapScene::onTapButton), this);
    CCCallFuncO* callbackAfterMinimize = CCCallFuncO::create(this, callfuncO_selector(HideAndSeekTapScene::onFinishMinimize), button);
    button->addCallbackAfterMinimize(callbackAfterMinimize);
    button->addCallback(callback);
    this->addChild(button);
    buttonArray->addObject(button);

}

void HideAndSeekTapScene::createRandomButtonsOnStart(int numberOfButtons){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.2f;
    int maxX = winSize.width - buttonSize;
    int maxY = winSize.height - scoreAndTimeLayer->getBarHeight() - buttonSize;


    for(int i=0;i<numberOfButtons;i++){
        int posX;
        int posY;
        CCRect iRect;
        bool hasIntersectSomeWhere;
        do {
            hasIntersectSomeWhere = false;
            posX = rand()%maxX;
            posY = rand()%maxY;
            iRect = CCRectMake(posX, posY, buttonSize, buttonSize);

            for(int j=0;j<buttonArray->count();j++){
                MySpecialDisappearButton* button = (MySpecialDisappearButton*)buttonArray->objectAtIndex(j);
                if(button->getBoundingRect().intersectsRect(iRect)){
                    hasIntersectSomeWhere = true;
                    break;
                }
            }
            
        } while (hasIntersectSomeWhere);

        MySpecialDisappearButton* button = MySpecialDisappearButton::create("circle_button.png", posX, posY, buttonSize, buttonSize);
        CCCallFuncO* callback = CCCallFuncO::create(this, callfuncO_selector(HideAndSeekTapScene::onTapButton), button);
        CCCallFuncO* callbackAfterMinimize = CCCallFuncO::create(this, callfuncO_selector(HideAndSeekTapScene::onFinishMinimize), button);
        button->addCallbackAfterMinimize(callbackAfterMinimize);
        button->addCallback(callback);
        this->addChild(button);
        buttonArray->addObject(button);
    }
}


#pragma mark - event handling
void HideAndSeekTapScene::onFinishMinimize(cocos2d::CCObject *sender){
    MySpecialDisappearButton* button = (MySpecialDisappearButton*)sender;
    this->removeChild(button);
    buttonArray->removeObject(button);
    
}


void HideAndSeekTapScene::onTapButton(cocos2d::CCObject *sender){
    createRandomButtonsOnStart(1);
    numberOfTap++;
    scoreAndTimeLayer->updateScore(numberOfTap);
}

void HideAndSeekTapScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){

        for(int j=0;j<buttonArray->count();j++){
            MySpecialDisappearButton* button = (MySpecialDisappearButton*)buttonArray->objectAtIndex(j);
            button->handleMultiTouchesBegan(touches, pEvent);
        }
    
}


void HideAndSeekTapScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){

        for(int j=0;j<buttonArray->count();j++){
            MySpecialDisappearButton* button = (MySpecialDisappearButton*)buttonArray->objectAtIndex(j);
            button->handleMultiTouchesMoved(touches, pEvent);
        }
}

void HideAndSeekTapScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){

        for(int j=0;j<buttonArray->count();j++){
            MySpecialDisappearButton* button = (MySpecialDisappearButton*)buttonArray->objectAtIndex(j);
            button->handleMultiTouchesEnded(touches, pEvent);
        }

}

